﻿using System.Windows;
using System.Windows.Input;

namespace YyMoney
{
    /// <summary>
    /// PagingDataGrid.xaml 的交互逻辑
    /// </summary>
    public partial class PagingDataGrid
    {
        /// <summary>
        /// 当前所在页
        /// </summary>
        public int PageIndex { get; set; }

        private int _pageSize;

        /// <summary>
        /// 每页行数
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                _pageSize = value;
                TxtPageSize.Text = value.ToString();
            }
        }

        /// <summary>
        /// 总行数
        /// </summary>
        public int RowCount { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount { get; set; }

        /// <summary>
        /// 获取或设置是否显示添加按钮
        /// </summary>
        public bool ShowAdd
        {
            get { return BtnAdd.Visibility == Visibility.Visible; }
            set { BtnAdd.Visibility = value ? Visibility.Visible : Visibility.Hidden; }
        }

        /// <summary>
        /// 获取或设置是否显示修改按钮
        /// </summary>
        public bool ShowModify
        {
            get { return BtnModify.Visibility == Visibility.Visible; }
            set { BtnModify.Visibility = value ? Visibility.Visible : Visibility.Hidden; }
        }

        /// <summary>
        /// 获取或设置是否显示删除按钮
        /// </summary>
        public bool ShowDelete
        {
            get { return BtnDelete.Visibility == Visibility.Visible; }
            set { BtnDelete.Visibility = value ? Visibility.Visible : Visibility.Hidden; }
        }

        /// <summary>
        /// 获取或设置是否显示查看按钮
        /// </summary>
        public bool ShowDetail
        {
            get { return BtnDetail.Visibility == Visibility.Visible; }
            set { BtnDetail.Visibility = value ? Visibility.Visible : Visibility.Hidden; }
        }

        /// <summary>
        /// 获取或设置是否显示刷新按钮
        /// </summary>
        public bool ShowUpdate
        {
            get { return BtnUpdate.Visibility == Visibility.Visible; }
            set { BtnUpdate.Visibility = value ? Visibility.Visible : Visibility.Hidden; }
        }

        //匹配数字和“Backspace”键
        const string RegexNum = @"^(D|NumPad)\d$|^Back$";

        public PagingDataGrid()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 跳到指定页
        /// </summary>
        /// <param name="pageIndex"></param>
        public void ToPage(int pageIndex)
        {
            if (pageIndex < 1 || pageIndex >= PageCount)
            {
                return;
            }


        }

        /// <summary>
        /// 更新表DgMoney的数据
        /// </summary>
        private void UpdateTable()
        {
            //DgMoney.ItemsSource = MySqlCommon.Instance.GetTable(string.Format(QueryDeal, 0, PageSize)).DefaultView;
        }

        public virtual void BtnAdd_Click(object sender, RoutedEventArgs e)
        {

        }

        public virtual void BtnDelete_Click(object sender, RoutedEventArgs e)
        {

        }

        public virtual void BtnModify_Click(object sender, RoutedEventArgs e)
        {

        }

        public virtual void BtnDetail_Click(object sender, RoutedEventArgs e)
        {

        }

        public virtual void BtnUpdate_Click(object sender, RoutedEventArgs e)
        {
            UpdateTable();
        }

        private void ImgFirstPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ToPage(1);
        }

        private void ImgPrevPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ToPage(PageIndex - 1);
        }

        private void ImgNextPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ToPage(PageIndex + 1);
        }

        private void ImgLastPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ToPage(PageCount);
        }

        private void TxtPageSize_MouseLeave(object sender, MouseEventArgs e)
        {
            int pageSize;

            try
            {
                pageSize = int.Parse(TxtPageSize.Text);
            }
            catch
            {
                TxtPageSize.Text = "";
                return;
            }

            if (pageSize <= 0)
            {
                TxtPageSize.Text = "";
                return;
            }

            _pageSize = pageSize;
        }

        private void TxtPageSize_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            

            if (!System.Text.RegularExpressions.Regex.IsMatch(e.Key.ToString(), RegexNum))
            {
                e.Handled = true;
            }
        }

        private void TxtPageSize_KeyDown(object sender, KeyEventArgs e)
        {
            
        }
    }
}
